package com.xtpt.disaster.controller;

import com.xtpt.disaster.bean.JobDetail;
import com.xtpt.disaster.bean.NodeInfo;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.common.consts.ScheduleTypeEnum;
import com.xtpt.disaster.common.consts.StatusConst;
import com.xtpt.disaster.common.consts.TaskTypeEnum;
import com.xtpt.disaster.controller.VO.FileRecoverInfo;
import com.xtpt.disaster.service.FileRecoverService;
import com.xtpt.disaster.service.JobDetailService;
import com.xtpt.disaster.service.NodeInfoManageService;
import com.xtpt.disaster.utils.IDGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author jack_xin
 * @ClassName FileRecoverController
 * @Desc 文件恢复接口
 * @date 2021年11月22日 10:30
 * @Version TODO
 */
@RestController
@CrossOrigin
@Api(tags = "文件恢复接口", value = "/filerecover")
@RequestMapping(value = "/filerecover")
public class FileRecoverController {
    @Autowired
    JobDetailService jobDetailService;

    @Autowired
    FileRecoverService fileRecoverService;

    @Autowired
    NodeInfoManageService nodeInfoManageService;

    /**
     * @Author jack xin
     * @Method Name fileTransRecover
     * @Desc 文件数据恢复，作业信息表写入
     * @Param [fileRecoverInfo]
     * @return com.xtpt.disaster.common.PortalResult
     * @Date 10:37 2021/11/23
    **/
    @ApiOperation(tags = "恢复方法，接收数据封装类fileRecoverInfo，根据jobId得到所需信息完成作业的写入", value = "/revcover", httpMethod = "POST", produces = "application/json")
    @PostMapping(value = "/recover")
    public PortalResult fileTransRecover(@RequestBody FileRecoverInfo fileRecoverInfo) {
        Long oldJobId = fileRecoverInfo.getJobId();
        Long newJobId = IDGenerator.longid();
        JobDetail job = jobDetailService.getById(oldJobId);
        if (job != null) {
            Long clientId = job.getNodeId();
            NodeInfo client = nodeInfoManageService.selectById(clientId);
            String clientPath = job.getClientPath();
            String serverPath = job.getServerPath();
            String jobName = fileRecoverInfo.getJobName();

            job.setJobName(jobName);
            job.setJobStartTime(new Date());
            job.setJobType(TaskTypeEnum.RECOVER.getValue());
            job.setScheduleType(ScheduleTypeEnum.IMMEDIATELY.getValue());
            job.setJobId(newJobId);
            job.setJobDesc(jobName);
            job.setJobBackupType(null);

            Integer taskType = job.getJobType();
            PortalResult ans = fileRecoverService.fileRecover(client, taskType, clientPath, serverPath);
            job.setJobEndTime(new Date());
            if (ans.isOK()) job.setJobStatus(StatusConst.SUCCESS);
            else job.setJobStatus(StatusConst.ERROR);
            String command = (String) ans.getData();
            job.setJobCommand(command);
            job.setJobResult(ans.getMsg());
            return jobDetailService.insert(job);
        }
        else return PortalResult.error("jobId不存在！");
    }

}
